<?php
class model_produce extends framework_base_model {
	/**
	 * @desc 添加大分类
	 * @param array 
	 * @return number
	 */
	public static function small_class_add($data) {
    	if(!is_array($data)) {
    		return false;
    	}
    	try{
			$basic = $data['basic_price'];
			unset($data['basic_price']);
    		$sql = "insert into `weixin_product` set ";
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$sql .= 'raw_add_time=NOW()';

			$sql1 = "insert into `weixin_basic_price` set ";
    		$binds1 = array();
    		foreach ($basic as $key => $value) {
				$sql2[$key] = $sql1;
				foreach($value as $k => $v){
					$sql2[$key] = $sql2[$key]."`{$k}` = '{$v}',";
				}
				$sql2[$key] = $sql2[$key].'`produce_id`=:produce_id,`raw_add_time` = NOW()';
    		}

			$pdo = self::DB() -> dbh;
			$pdo->beginTransaction();
    		$stm = $pdo-> prepare($sql);
    		$stm->execute($binds);
			$last_id = $pdo->lastInsertId();
			foreach($sql2 as $v){
				$stm1 = $pdo-> prepare($v);
    			$stm1->execute(array(':produce_id' => $last_id));
			}
			$pdo->commit();
			return true;
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
    }
    /*
     * 根据名称判断是否存在该分类名称
     */
    public static function get_class_by_name($class_name, $id = 0) {
    	$info = array();
		$class_name = trim($class_name);
    	if(empty($class_name)) {
    		return $info;
    	}
    	$sql = "select `product_class_id`,`class_name`,`status`,`raw_add_time`,`raw_update_time`,`author`,`editer` from `weixin_product_class` where `class_name`=:class_name and `delete_flag` = 0";
		$binds[":class_name"] = $class_name;
		if(!empty($id)){
			$sql .= " and `product_class_id` != :product_class_id";
			$binds[":product_class_id"] = $id;
		}
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute($binds);
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
	/*
     * 根据id是否存在该分类名称
     */
    public static function get_produce_by_id($id) {
    	$info = array();
		$id = intval($id);
    	if(empty($id)) {
    		return $info;
    	}
    	$sql = "select `product_id`,`produce_name`,`parent_id`,`area_motor_id`,`area_smotor_id`,`area_ship_id`,`area_train_id`,`raw_add_time`,`raw_update_time`,`status`,`author`,`editer` from `weixin_product` where `product_id`=:product_id and delete_flag = 0";
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute(array(":product_id" => $id));
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
    /**
     * @desc 更新分类
     * @param unknown $id
     * @param unknown $data
     * @return unknown|number
     */
    public static function update_produce_info($id,$data) {
		if(empty($data) || empty($data['basic_price'])){
			return false;
		}
    	try{
			//$trsanport = $data['transport'];
			$basic = $data['basic_price'];
			unset($data['basic_price']);
    		$sql = 'UPDATE `weixin_product` SET ';
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$sql = trim($sql,',');
    		$sql .= " WHERE `product_id` = {$id}";
			$pdo = self::DB() -> dbh;
			$pdo->beginTransaction();
    		$stm= $pdo-> prepare($sql);
    		$stm->execute($binds);
			$sql = "select `basic_price_id` from `weixin_basic_price` where `produce_id` = :produce_id";

			$stm= $pdo-> prepare($sql);
			$stm->execute(array(':produce_id' => $id));
			$raws = $stm->fetchAll();

			$sql1 = "update `weixin_basic_price` set `marerial`=:marerial,`markup`=:markup,`show_sort`=:show_sort,`delete_flag`=0 where `basic_price_id`=:basic_price_id";
			$sql2 = "insert into `weixin_basic_price` set `marerial`=:marerial,`markup`=:markup,`show_sort`=:show_sort,`produce_id`=:produce_id";
			$sql3 = "update `weixin_basic_price` set `delete_flag` = 1 where `produce_id`=:produce_id";
			$stm= $pdo-> prepare($sql3);
			$stm->execute(array(':produce_id' => $id));
			foreach($basic as $key => $value){
				$binds = array();
				foreach($value as $k => $v){
					$binds[":{$k}"] = $v;
				}
				if(!empty($raws[$key])){
					$binds[":basic_price_id"] = $raws[$key]['basic_price_id'];
					$stm= $pdo-> prepare($sql1);
				}else{
					$binds[":produce_id"] = $id;
					$stm= $pdo-> prepare($sql2);
				}
				$stm->execute($binds);
			}
			$pdo->commit();
    		return true;
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
    }
	//删除小分类
	public static function delete_product($id){
		if(empty($id)){
			return false;
		}
		$sql1 = "update `weixin_product` set `delete_flag` = 1 where `product_id` in ({$id})";
		$sql2 = "update `weixin_basic_price` set `delete_flag` = 1 where `produce_id`  in ({$id})";
		try {
			$pdo = self::DB()->dbh;
			$pdo->beginTransaction();
    		$st = $pdo->prepare($sql1);
    		$st->execute();
			$st = $pdo->prepare($sql2);
    		$st->execute();
    		$pdo->commit();
			return true;
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
			return false;
    	}
	}
    /**
     * 获取大分类列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function get_small_class_list($nextNum = 0,$pagesize = 0,$condition = array()) {
		$filed = "a.`product_id`,a.`produce_name`,a.`parent_id`,a.`area_motor_id`,a.`area_smotor_id`,a.`area_ship_id`,a.`area_train_id`,a.`raw_add_time`,a.`raw_update_time`,b.`class_name`,a.`status`,a.`author`,a.`editer`";
    	$sql = "select {$filed} from weixin_product a left join weixin_product_class b on a.`parent_id`=b.`product_class_id` where a.`delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['parent_id'])) {
    			$where .= " and a.`parent_id` = {$condition['parent_id']}";
    		}
			if(isset($condition['class_small_name'])) {
    			$where .= " and a.`produce_name` like '%{$condition['class_small_name']}%'";
    		}
    	}
    	$where .= " order by a.`raw_add_time` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		
		$sql = "select count(a.`product_id`) from weixin_product a left join weixin_product_class b on a.`parent_id`=b.`product_class_id` where a.`delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	/*
	* @desc 获取大分类列表
	* @param 
	* @return array()
	**/
	public static function get_type_list(){
		$sql = "select `product_class_id`,`class_name` from `weixin_product_class` where `delete_flag` = 0 and `status` = 0";
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		return $result;
	}
	/*
	* @desc 获取地区
	* @param 
	* @return array()
	**/
	/*public static function get_area_list(){
		$return = array();
		$sql = "select `area_id`,concat(`province` , '/', `city` , '/', `area`, '/', `street`) as name,`sortkey` from `weixin_area` where `delete_flag` = 0 order by `sortkey`";
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		return $result;
	}*/
	public static function get_area_list(){
		$return = array();
		$sql = "select `area_id`,`province`,`city`,`area`,`street`,`sortkey` from `weixin_area` where `delete_flag` = 0 order by `sortkey`";
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		return $result;
	}
	//获取基础价信息
	public static function get_basic_price($produce_id){
		$return = array();
		if(empty($produce_id)){
			return $return;
		}
		$sql = "select `basic_price_id`,`produce_id`,`marerial`,`markup`,`show_sort` from `weixin_basic_price` where `produce_id` = :produce_id and `delete_flag` = 0";
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute(array(":produce_id" => $produce_id));
    		$return = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
		return $return;
	}
	//获取运输方式的省份
	public static function get_transport_province($id, $name){
		$return = array();
		if(empty($id) || empty($name)){
			return $return;
		}
		$name = htmlspecialchars($name);
		$sql = "select `{$name}` from `weixin_product` where `product_id` = :product_id";
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute(array(":product_id" => $id));
    		$return = $st->fetchColumn();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
		return $return;
	}
	//根据模板ID获取商品信息
	public static function get_produce_by_tmpid($tmpid){
		$return = array();
		if(empty($tmpid)){
			return $return;
		}
		$sql = "select `product_id` from `weixin_product` where ( `area_motor_id` = :area_motor_id or `area_smotor_id`=:area_smotor_id or `area_ship_id`=:area_ship_id or `area_train_id`=:area_train_id ) and `delete_flag` = 0";
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute(array(":area_motor_id" => $tmpid,":area_smotor_id" => $tmpid,":area_ship_id" => $tmpid,":area_train_id" => $tmpid));
    		$return = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
		return $return;
	}
}
